//删除的id
var delId = [];
var saveDataFlg = 0;
//	时间初试化
$('#startTime').datetimepicker({
	minView: "month",
	language: 'zh-CN',
	format: 'yyyy-mm-dd',
	todayBtn: 1,
	autoclose: true
});
$('#endTime').datetimepicker({
	minView: "month",
	language: 'zh-CN',
	format: 'yyyy-mm-dd',
	todayBtn: 1,
	autoclose: true
});
var oprateUrl = DOMAIN + '/v1/inventory' + isLogin;
function loadTable() {
	$("#table").bootstrapTable({ // 对应table标签的id
		url: oprateUrl,
		method: 'get',
		cache: false,
		striped: true,
		pagination: true,
		pageSize: 50,
		pageNumber: 1,
		pageList: [50,100],
		search: false,
		showColumns: false,
		clickToSelect: true,
		queryParams: {
			deleteFlg: 0
		},
		smartDisplay: false,
		columns: [{
			field: "id",
			title: "id",
			align: "center",
			valign: "middle",
			sortable: "true",
			visible: false
		}, {
			field: "storageNo",
			title: "入库单号",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "refundCode",
			title: "来源单号",
			align: "center",
			valign: "middle",
			sortable: "true"
		},{
			field: "customerName",
			title: "货物来源",
			align: "center",
			valign: "middle",
			sortable: "true"
		},{
			field: "storageType",
			title: "入库类型",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "storageDate",
			title: "入库日期",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "productCode",
			title: "产品代码",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "productName",
			title: "产品名称",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "productType",
			title: "产品类别",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "grade",
			title: "产品级别",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "units",
			title: "单位",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "storageAmount",
			title: "入库数量",
			align: "center",
			valign: "middle",
			sortable: "true"
		}, {
			field: "inventoryAmount",
			title: "库存数量",
			align: "center",
			valign: "middle",
			sortable: "true",
			cellStyle:function(value,row,index){
				return {css:{"color":"#0088cc"}} 
			}
		}, {
			field: "createUser",
			title: "操作员",
			align: "center",
			valign: "middle",
			sortable: "true"
		}/*,{
			field: "",
			title: "操作",
			align: "center",
			valign: "middle",
			events: funOprate,
			formatter: function (value, row, index) {
				if (row.deleteFlg == 1) {
					return '<button storageNo="' + row.storageNo + '" type="button" data-toggle="modal" data-edit="myModal" data-target="#myModal" onclick="EditInfo(this)" class="btn btn-primary btn-sm edit">编辑</button>';
				} else {
					return '<button storageNo="' + row.storageNo + '" type="button" data-toggle="modal" data-edit="myModal" data-target="#myModal" onclick="EditInfo(this)" class="btn btn-primary btn-sm edit">&emsp;查看</button> <button data-del="' + oprateUrl + '" class="btn btn-primary btn-sm del">&emsp;删除</button>';
				}
			}
		}*/],
		//>>>>>>>>>>>>>>导出excel表格设置
		showExport: true,
		showExport: true,
		//是否显示导出按钮
		buttonsAlign: "left",
		//按钮位置
		exportDataType: 'all',
		//导出的方式 all全部 selected已选择的  basic', 'all', 'selected'.
		Icons: 'glyphicon glyphicon-export',
		exportTypes: ['excel', 'doc', 'xlsx', 'txt',],
		//导出文件类型
		exportOptions: {
			ignoreColumn: [0, 0],  //忽略某一列的索引，id删除更新用用，不显示与导出

			fileName: '导出数据表',
			//文件名称设置
			worksheetName: 'sheet1',
			//表格工作区名称
			tableName: '导出数据表',
			// excelstyles: ['background-color', 'color', 'font-size', 'font-weight'], 设置格式

		},
		//导出excel表格设置<<<<<<<<<<<<<<<<
		formatNoMatches: function () {
			return '没有找到匹配的记录';
		},
		onClickCell: function(field, value, row, $element) {
			saveDataFlg=0;
			if(field=="inventoryAmount"){
	    		$element.attr('contenteditable', true);
	       		$element.blur(function() {
	       			if(saveDataFlg == 0){
		       			if($element.html()=="" || isNaN($element.html()) || $element.html()<0){
		       					alert("请输入不小于0的数字");
		       					$element.html(value);
		       			}else{
		       				let index = $element.parent().data('index');
		            		let tdValue = $element.html();
		            		saveData(value, index, field, tdValue,row);
		       			}
	       			}
	       			saveDataFlg++;
	       		});
	    	}
		}
	});
}

 function saveData(value,index, field, tdValue,row) {
	var params = {};
	params.id = row.id;
	params.newInventoryNum = tdValue;
	params.inventoryAmount = value;
	params.productCode = row.productCode;
	console.log(params);
	params = JSON.stringify(params);
    $.ajax({
			type: "post",
			url: DOMAIN + '/v1/inventory' + isLogin,
			data: params,
			dataType: 'JSON',
			contentType: "application/JSON; charset=utf-8",
			success: function (result) {
				if (result.status == 200) {
					$("#table").bootstrapTable('updateCell', {
					    index: index,       
					    field: field,       //列名
					    value: tdValue        //cell值
				    });
				    return;
				}
				if (result.status == 101) {
					$("#table").bootstrapTable('updateCell', {
					    index: index,       
					    field: field,       //列名
					    value: value        //cell值
					});
					alert(result.msg);
					return;
				}else{
					$("#table").bootstrapTable('updateCell', {
					    index: index,       
					    field: field,       //列名
					    value: value        //cell值
					});
					alert('修改失败');
				}
			},
			error: function () {
				$("#table").bootstrapTable('updateCell', {
					    index: index,       
					    field: field,       //列名
					    value: value        //cell值
				});
				alert('修改失败');
			}
		});
}

//查询
$("#selectPurchaseBtn").click(function () {
	//新查询参数
	function queryParamNew(params) {
		var params = {};
		params.startTime = $("#startTime").val();
		params.endTime = $("#endTime").val();
		params.storageType = $("#storageType option:checked").val();
		return params;
	};
	var url = DOMAIN + '/v1/inventory' + isLogin;
	$('#table').bootstrapTable(
		'refreshOptions',
		{
			url: url,
			//查询参数
			queryParams: queryParamNew
		}
	);
})

$(function(){
	loadTable();
});
